<template>
  <div class="goods page" ref="webview">
    <div ref="wrapper">
      <van-sticky @scroll="scrollNavBar" ref="sticky">
        <van-nav-bar v-if="isNavFixed" class="bg_white" :title="goodsInfo.goods_name" left-arrow @click-left="$router.go(-1)" />
      </van-sticky>
      <div class="swipe">
        <div class="van-icon van-icon-arrow-left van-nav-bar__arrow" v-if="!isNavFixed" @click="$router.go(-1)"></div>
        <div class="video_popup-close van-icon van-icon-cross" v-if="isVideoPopup" @click="VideoPopupClose"></div>
        <van-swipe :autoplay="3000" class="home-swipe" @change="onChange">
          <van-swipe-item v-for="(item, index) in imageList" :key="index">
            <div class="img">
              <van-image width="100%" :height="clientWidth" fit="contain" lazy-load :src="item.img_path">
                <template v-slot:loading>
                  <van-loading type="spinner" size="24" />
                </template>
              </van-image>
            </div>
          </van-swipe-item>
          <div class="custom-indicator" slot="indicator">
            {{ current + 1 }}/{{imageList.length}}
          </div>
        </van-swipe>
      </div>
      <div class="goods-info">
        <div class="g-price-inventory">
          <span class="g-price">￥{{goodsInfo.min_price}}
            <!-- <span class="text">配送积分</span> -->
          </span>
          <!-- <span class="g-inventory">库存{{goodsInfo.total_stock}}</span> -->
        </div>
        <div class="g-title">{{goodsInfo.goods_name}}</div>
        <!-- <div class="g-describe">【小米开工大吉!采购有礼(限产品)!】【京仓速发,自营同款放心选购】【小米品牌授权，品质保证】欢迎咨询客服哦！</div> -->
      </div>
      <div class="goods-specifications" @click="changeSku">
        <div class="g-left">
          <span class="name">已选</span>
          <span class="info">{{firstNormName}}</span>
        </div>
        <div class="g-more">
          <span class="dot"></span>
          <span class="dot r"></span>
          <span class="dot"></span>
        </div>
      </div>
      <div class="content">
        <div class="title van-hairline--bottom">
          <span>商品详情</span>
        </div>
        <div class="box" v-html="goodsInfo.detail">
        </div>
      </div>
      <div class="g-bottom-bar">
        <div class="bar-fixd van-hairline--top">
          <span class="name">￥{{goodsInfo.min_price}}
            <!-- <span class="text">配送积分</span> -->
          </span>
          <van-button color="#007AFF" class="btn" @click="isPopup=true">立即兑换</van-button>
        </div>
      </div>
      <van-popup v-model="isPopup" position="bottom" style="overflow-y: unset;">
        <div class="g-sku-container">
          <div class="g-sku-header">
            <div class="g-sku-header__img-wrap">
              <van-image width="100%" :height="$tools.toPX(200)" fit="contain" lazy-load :src="goodsInfo.goods_img">
                <template v-slot:loading>
                  <van-loading type="spinner" size="24" />
                </template>
              </van-image>
            </div>
            <div class="g-sku-header__goods-info">
              <div class="g-sku__goods-price">￥{{firstNormPrice}}
                <!-- <span class="text">配送积分</span> -->
              </div>
              <div class="g-sku__goods-item">已选：{{firstNormName}}</div>
            </div>
            <span class="van-icon van-icon-cross" @click="isPopup=false"></span>
          </div>
          <div class="g-sku-body">
            <div class="g-sku-body__item">
              <div class="g-sku-body__item-title">规格</div>
              <j-radio-group v-model="firstNormName" @change="changeFirstNormName">
                <template v-for="(item,index) in normList">
                  <j-radio :label="item.norm_name" :key="index" :value="item"></j-radio>
                </template>
              </j-radio-group>
            </div>
            <div class="g-sku-body__item g-sku-body__item_flex">
              <div class="g-sku-body__item-title">数量</div>
              <div class="g-sku-body__inventory">
                <van-stepper v-model="buyNumber" min="1" :max="stock" />
                <!-- <span class="info">剩余{{stock}}件商品</span> -->
              </div>
            </div>
          </div>
        </div>
        <div class="g-sku-actions">
          <van-button color="#007AFF" round class="btn" @click="exchange">立即兑换</van-button>
        </div>
      </van-popup>
    </div>
  </div>
</template>
<script>
export default {
  name: 'goods',
  data () {
    return {
      clientWidth: document.body.clientWidth,
      isPopup: false,
      firstNormName: '',
      firstNormPrice: '',
      stock: 0,
      buyNumber: 1,
      current: 0,
      isNavFixed: false,
      goodsInfo: {},
      normList: [],
      imageList: [],
      isVideoPopup: false
    };
  },
  // components: {
  //   Xgplayer
  // },
  destroyed () {
    this.Player = null;
  },
  mounted () {
    this.getGoodsInfo();
  },
  methods: {
    scrollNavBar (scrollTop) {
      this.isNavFixed = scrollTop.isFixed;
    },
    VideoPopupClose () {
      this.isVideoPopup = false;
      this.Player.pause();
    },
    getGoodsInfo () {
      this.$toast.loading({ duration: 0 });
      this.$API.global.goodsInfo({ id: this.$route.query.id }).then(res => {
        if (!res.code) {
          this.firstNormName = res.data.firstNormName;
          this.firstNormPrice = res.data.firstNormPrice;
          this.normList = res.data.normList;
          this.goodsInfo = res.data.goods;
          this.imageList = res.data.imageList;
        }
        this.$toast.clear();
      });
    },
    openVideo () {
      this.isVideoPopup = true;
      this.Player.play();
    },
    onChange (index) {
      this.current = index;
    },
    changeFirstNormName (data) {
      this.firstNormName = data.norm_name;
      this.firstNormPrice = data.norm_price;
      this.stock = data.stock;
      this.normId = data.id;
    },
    changeSku () {
      this.isPopup = true;
    },
    exchange () {
      if (!localStorage.getItem('Token')) {
        this.$store.dispatch('setLogin', true);
        return;
      }

      if (!this.normId) {
        this.$toast('必须选择一个规格');
        return;
      }
      this.$router.push({ 'path': '/order/confirm', query: { nid: this.normId, num: this.buyNumber } });
    }
  }
};
</script>
<style lang="less" scoped>
.goods {
  background-color: #fff;
  .swipe {
    position: relative;
    .video_popup-close {
      width: 66px;
      height: 66px;
      border-radius: 66px;
      background-color: rgba(0, 0, 0, 0.3);
      display: flex;
      color: #fff;
      justify-content: center;
      align-items: center;
      position: absolute;
      top: 20px;
      left: 110px;
      font-size: 34px;
      z-index: 999;
    }
    .video_time {
      position: absolute;
      bottom: 30px;
      left: 0;
      text-align: center;
      right: 0;
      .time {
        margin-left: 20px;
        height: 60px;
        display: inline-block;
        padding: 10px 20px 10px 65px;
        background-color: #fff;
        font-size: 28px;
        border: 1px solid rgba(230, 230, 230, 1);
        opacity: 0.8;
        border-radius: 30px;
        position: relative;
        &::before {
          content: '';
          display: block;
          width: 46px;
          height: 46px;
          position: absolute;
          left: 5px;
          top: 5px;
          background-image: url('/static/images/icon-play.png');
          background-repeat: no-repeat;
          background-size: cover;
        }
      }
    }
    .video_popup {
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      visibility: hidden;
    }
    .video_popup.active {
      visibility: visible;
    }
    .custom-indicator {
      position: absolute;
      bottom: 20px;
      right: 20px;
      padding: 2px 15px;
      background-color: rgba(0, 0, 0, 0.3);
      border-radius: 20px;
      color: #fff;
      font-size: 24px;
    }
    .van-nav-bar__arrow {
      width: 66px;
      height: 66px;
      position: absolute;
      top: 20px;
      left: 20px;
      z-index: 999;
      font-size: 34px;
      background-color: rgba(0, 0, 0, 0.3);
      border-radius: 66px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      &::before {
        margin-left: -5px;
      }
    }
  }
  .goods-info {
    padding: 20px 40px 24px;
    .g-price-inventory {
      .g-price {
        color: #ff0034;
        font-size: 48px;
      }
      .text {
        color: #ff0034;
        font-size: 24px;
        vertical-align: middle;
      }
    }
    .g-title {
      color: #353535;
      font-size: 36px;
      padding: 10px 0 20px;
    }
    .g-describe {
      color: #969aa6;
    }
  }
  .goods-specifications {
    border-top: 20px solid #f1f3f7;
    border-bottom: 20px solid #f1f3f7;
    padding: 32px 42px;
    color: #353535;
    font-size: 28px;
    display: flex;
    justify-content: space-between;
    .g-left {
      flex: 1;
      display: flex;
      padding-right: 10px;
    }
    .name {
      font-weight: 500;
      margin-right: 30px;
    }
    .info {
      flex: 1;
    }
    .g-more {
      .dot {
        display: inline-block;
        width: 10px;
        height: 10px;
        background-color: #353535;
        border-radius: 10px;
        // margin-right: 8px;
      }
      .r {
        margin: 0 8px;
      }
      .dot:first-child {
        opacity: 0.3;
      }
    }
  }
  .g-bottom-bar {
    height: 100px;

    .bar-fixd {
      position: fixed;
      background-color: #fff;
      bottom: 0;
      left: 0;
      right: 0;
      height: 90px;
      display: flex;
      z-index: 99;
      justify-content: space-between;
      align-items: center;
      .btn {
        width: 250px;
        font-size: 32px;
      }
      .name {
        color: #ff0034;
        font-size: 36px;
        padding-left: 30px;
        line-height: 90px;
        .text {
          font-size: 24px;
          color: #ff0034;
          margin-left: 2px;
          position: relative;
          top: -3px;
        }
      }
    }
  }
  .content {
    padding: 0 24px;
    background-color: #fff;
    .box {
      padding: 20px 14px 40px;
      line-height: 40px;
      font-size: 28px;
      .img {
        margin: 20px 0;
        img {
          width: 100%;
          height: auto;
        }
      }
    }
    .title {
      height: 90px;
      margin: 0 15px;
      span {
        display: inline-block;
        font-size: 30px;
        padding: 22px 0 23px;
        position: relative;
        &::before {
          content: '';
          display: inline-block;
          border-bottom: 4px solid #917eff;
          position: absolute;
          bottom: -2px;
          left: 0;
          right: 0;
          z-index: 99;
        }
      }
    }
  }
  .g-sku-container {
    display: flex;
    padding: 0 30px 30px;
    box-orient: vertical;
    box-direction: normal;
    direction: column;
    flex-direction: column;
    box-align: stretch;
    align-items: stretch;
    overflow-y: visible;
    background: #fff;
    .g-sku-header {
      .van-icon {
        width: 30px;
        height: 30px;
        font-size: 34px;
        position: absolute;
        top: 30px;
        right: 30px;
        color: #888;
      }
      .g-sku-header__img-wrap {
        width: 200px;
        height: 200px;
        float: left;
        background-color: #eee;
        margin-top: 35px;
        img {
          width: 200px;
          height: 200px;
          object-fit: contain;
        }
      }
      .g-sku-header__goods-info {
        display: flex;
        box-orient: vertical;
        box-direction: normal;
        flex-direction: column;
        box-pack: end;
        min-height: 110px;
        padding: 20px 0 0 30px;
        .g-sku__goods-price {
          font-size: 48px;
          color: #ff0034;
          position: relative;
          .text {
            vertical-align: middle;
            font-size: 24px;
          }
        }
        .g-sku__goods-item {
          color: #353535;
          padding-bottom: 15px;
        }
        justify-content: flex-end;
      }
    }
    .g-sku-body__item-title {
      font-size: 24px;
      color: #353535;
      padding: 25px 0;
      font-weight: 500;
    }
    .g-sku-body__inventory {
      display: flex;
      align-items: center;
      .info {
        color: #969aa6;
        margin-left: 20px;
      }
    }
  }
  .g-sku-actions {
    margin-top: 10px;
    padding: 20px 30px 40px;
    .btn {
      width: 100%;
      height: 100px;
      font-size: 34px;
    }
  }
}
</style>
